/**
 * Created by Administrator on 2017/4/11.
 */
/**
 * Pagination组件
 *
 * @class Pagination
 * @extends Basic
 *
 * @param [pageVal] 包括（totalPage: 总页数 pageSize:每页显示条数 curPage: 当前页码）
 *
 * 示例:
 *
 *     @example
 *
 *     var pageVal = {
 *               totalPage: 4,	//总页数
 *               pageSize: 2,	//每页显示条数
 *               curPage: 1	//当前页码
 *           };
 *
 *     var pageClick = function(){
 *          console.log(123);
 *     }
 *
 *     <Pagination  pageVal={pageVal} onClick={pageClick} />
 */
var Pagination = React.createClass({
    getDefaultProps: function () {
      return {
          pageVal:{}
      }
    },
    getInitialState: function () {
        return{
            curPage:this.props.pageVal.curPage
        }
    },
    onClick: function (curPage) {
        this.setState({curPage:curPage});
        this.props.onClick && this.props.onClick(curPage);
    },
    render: function(){
        var _this = this;
        var pageVal = _this.props.pageVal;
        var curPage = _this.state.curPage;
        var div = [];
        if(pageVal.totalPage > 1 && pageVal.totalPage <= 7){
            for(var i = 0; i <= pageVal.totalPage+1; i++){
                var a = document.createElement("a");
                a.setAttribute("href","javascript:;");
                if(i==0){
                    a.innerHTML = "&lt;";
                    if(curPage > 1){
                        a.onclick = function(){
                            _this.onClick(curPage-1);
                        };
                    }else{
                        a.className = "pagination-disabled";
                    }
                }else if(i == pageVal.totalPage+1){
                    a.innerHTML = "&gt;";
                    if(curPage < pageVal.totalPage){
                        a.onclick = function(){
                            _this.onClick(curPage+1);
                        };
                    }else{
                        a.className = "pagination-disabled";
                    }
                }else{
                    a.innerHTML = i;
                    (function(ii){
                        a.onclick = function(){
                            _this.onClick(ii);
                        };
                    })(i);
                    if(i == curPage){
                        a.className = "current";
                    }
                }
                div.push(a);
            }
        }else if(pageVal.totalPage > 7){
            var dotIndex = 0;
            if(curPage<=5 || (pageVal.totalPage - curPage) <= 4 ){
                if((curPage - 1) < (pageVal.totalPage - curPage)){
                    dotIndex = 6;
                }else{
                    dotIndex = 2;
                }
            }
            for(var i = 0; i < 9; i++){
                var a = document.createElement("a");
                a.setAttribute("href","javascript:;");
                if(i==0){
                    a.innerHTML = "&lt;";
                    if(curPage > 1){
                        a.onclick = function(){
                            _this.onClick(curPage-1);
                        };
                    }else{
                        a.className = "pagination-disabled";
                    }
                }else if(i == 8){
                    a.innerHTML = "&gt;";
                    if(curPage < pageVal.totalPage){
                        a.onclick = function(){
                            _this.onClick(curPage+1);
                        };
                    }else{
                        a.className = "pagination-disabled";
                    }
                }else{
                    if(dotIndex == 0){
                        if(i == 2 || i == 6){
                            if(i == 2){
                                a.onclick = function(){
                                    _this.onClick(curPage-3);
                                };
                                a.className = "jump-prev";
                                a.setAttribute("title","Previous 3 Pages");
                            }else if(i == 6){
                                a.onclick = function(){
                                    _this.onClick(curPage+3);
                                };
                                a.className = "jump-next";
                                a.setAttribute("title","Next 3 Pages");
                            }
                        }else{
                            if(i == 1){
                                a.innerHTML = 1;
                                a.onclick = function(){
                                    _this.onClick(1);
                                };
                            }else if(i == 7){
                                a.innerHTML = pageVal.totalPage;
                                a.onclick = function(){
                                    _this.onClick(pageVal.totalPage);
                                };
                            }else if(i == 3){
                                a.innerHTML = curPage-1;
                                a.onclick = function(){
                                    _this.onClick(curPage-1);
                                };
                            }else if(i == 4){
                                a.innerHTML = curPage;
                                a.onclick = function(){
                                    _this.onClick(curPage);
                                };
                                a.className = "current";
                            }else if(i == 5){
                                a.innerHTML = curPage+1;
                                a.onclick = function(){
                                    _this.onClick(curPage+1);
                                };
                            }
                        }
                    }else if(dotIndex == 2){
                        if(i == dotIndex){
                            a.onclick = function(){
                                _this.onClick(curPage-3);
                            };
                            a.className = "jump-prev";
                            a.setAttribute("title","Previous 3 Pages");
                        }else{
                            if(i == 1){
                                a.innerHTML = 1;
                                a.onclick = function(){
                                    _this.onClick(1);
                                };
                            }else{
                                a.innerHTML = (pageVal.totalPage-(7-i));
                                (function(i){
                                    a.onclick = function(){
                                        _this.onClick(pageVal.totalPage-(7-i));
                                    };
                                })(i);
                            }
                        }
                        if((pageVal.totalPage-(7-i)) == curPage){
                            a.className = "current";
                        }
                    }else if(dotIndex == 6){
                        if(i == dotIndex){
                            a.onclick = function(){
                                _this.onClick(curPage+3);
                            };
                            a.className = "jump-next";
                            a.setAttribute("title","Next 3 Pages");
                        }else{
                            if(i == 7){
                                a.innerHTML = pageVal.totalPage;
                                a.onclick = function(){
                                    _this.onClick(pageVal.totalPage);
                                };
                            }else{
                                a.innerHTML = i;
                                (function(i){
                                    a.onclick = function(){
                                        _this.onClick(i);
                                    };
                                })(i);
                            }
                        }
                        if(i == curPage){
                            a.className = "current";
                        }
                    }
                }
                div.push(a);
            }
        }
        return (
            <div className="pagination">
                {div}
            </div>
        )
    }
});

module.exports = Pagination;